<template>
	<view>
		<view class="box-pading" v-show="type">

			<view class="title">
				验证身份信息
			</view>
			<view class="subhead f-base">
				<!-- 请输入{{$store.state.userInfo.name}}的身份证号码 -->
				验证身份信息
			</view>

			<set-input placeholder="请输入您的身份证号码" @input="changeIdCard" class="m-b-30" type="idcard"></set-input>

			<view>
				<button type="default" class="btn flex-no-horizontal f-base" @click="nextStep">下一步</button>
			</view>

			<view class="bottom f-base" @click="toCustomerService">
				其他认证方式
			</view>

		</view>

		<view class="box-pading" v-show="!type" style="margin-top: 40rpx;">
			<set-input placeholder="请输入6位数字安全密码" @input="changePassword" class="m-b-15"></set-input>
			<set-input placeholder="再次输入密码" @input="changePasswordAgin" class="m-b-30"></set-input>
			<u-button shape="circle" color="#000000" size="large" @click="confirm">确认</u-button>
		</view>

	</view>
</template>

<script>
	const permissionUtils = require('@/common/permission.js')
	import setInput from './common/setInput.vue'
	import {
		face
	} from '../../utility/face.js'
	import {checkIdCardApi,setPasswordApi,checkFaceApi} from '../../api/collection.js'
	import { getQiniuData,uploadQiniu } from '../../utility/upload.js'
	import md5 from 'js-md5';
	export default {
		components: {
			setInput
		},
		data() {
			return {
				idCard: '',
				password: '',
				newPassword: '',
				type: true
			}
		},

		methods: {
			
			//身份证
			changeIdCard(e) {
				this.idCard = e
			},

			//下一步
			async nextStep() {
				try{
					//判断是否为身份证
					if (!uni.$u.test.idCard(this.idCard)) {
						uni.showToast({
							title: '请输入正确的身份证',
							icon: 'none'
						})
						return
					}
					//校验身份证
					const idCardRes = await checkIdCardApi({
						idCard:this.idCard
					})
					//调用人脸
					const faceRes = await face()
					uni.showLoading({
						title:'正在验证人脸',
						mask:true
					})
					//七牛token
					const qiniuRes = await getQiniuData()
					//七牛上传
					const url = await uploadQiniu(qiniuRes.data,{path:faceRes,name: parseInt(Math.random() * (999999 - 100000 + 1) + 100000, 10)})
					//检验人脸
					const checkFaceRes = await checkFaceApi({
						contrast:qiniuRes.data.domain + url.key
					})
					uni.hideLoading()
					uni.showToast({
						title:'人脸校验通过',
						icon:'none'
					})
					this.type = !this.type
				}catch(e){
					uni.hideLoading()
					uni.showToast({
						title: e.message || e.data,
						icon: 'none'
					})
					if (e.code === -5 && plus.os.name === 'Android') {
						this.showModal({
							title: '权限受限',
							msg: '您拒绝了一些权限，可能导致该服务无法正常使用，请前往设置！',
							confirmText: '前往设置',
						}).then(() => {
							permissionUtils.gotoAppPermissionSetting()
							uni.navigateBack()
						}).catch((err) =>{
							uni.navigateBack()
						})
					}else{
						setTimeout(()=> {
							uni.navigateBack()
						}, 800);
					}
				}

			},

			//其他身份认证方式
			toCustomerService() {
				uni.navigateTo({
					url: './customerService'
				})
			},

			//新密码
			changePassword(val) {
				this.password = val
			},
			//确认密码
			changePasswordAgin(val) {
				this.newPassword = val
			},

			//重置密码确定
			confirm() {
				switch (true) {
					case !this.password || !this.newPassword:
						uni.showToast({
							title: '安全密码或确认密码不能为空',
							icon: 'none'
						})
						break;
					case !uni.$u.test.number(this.password) || this.password.length !== 6:
						uni.showToast({
							title: '请输入6位数字安全密码',
							icon: 'none'
						})
						break;
					case this.password !== this.newPassword:
						uni.showToast({
							title: '两次输入密码不一致',
							icon: 'none'
						})
						break;
					default:
						console.log('验证码成功')
						this.modilfyPassword()
						break;
				}
			},
			
			async modilfyPassword(){
				try{
					uni.showLoading({
						title:'请稍后',
						mask:true
					})
					const modifyRes = await setPasswordApi({
						code: md5(this.$store.state.basePassword + this.password),
						repeat:md5(this.$store.state.basePassword + this.newPassword),
						type:3
					})
					uni.hideLoading()
					uni.showToast({
						title: '重置成功',
						icon: 'none'
					})
					setTimeout(()=> {
						uni.navigateBack({
							delta:2
						})
					}, 2000);
				}catch(e){
					uni.hideLoading()
					//TODO handle the exception
					uni.showToast({
						title: e.message || e.data,
						icon: 'none'
					})
				}
			},
			
			/**
			 * 显示弹窗提示
			 */
			showModal({
				title,
				msg,
				confirmText
			}) {
				return new Promise((resolve, reject) => {
					uni.showModal({
						title,
						content: msg,
						showCancel: true,
						confirmText: confirmText || '确认',
						success(res) {
							if (res.confirm) {
								resolve()
							} else {
								reject()
							}
						},
						fail() {
							reject()
						}
					})
				})
			}

		}

	}
</script>

<style lang="scss" scoped>
	
	.box-pading{
		.title {
			text-align: center;
			font-size: 40rpx;
			color: #000000;
			margin-top: 140rpx;
		}
		
		.subhead {
			text-align: center;
			color: rgba(97, 99, 113, 1);
			margin: 30rpx 0 60rpx 0;
		}
		
		.btn {
			justify-content: center;
			border-radius: 49.5rpx;
			height: 100rpx;
			background-color: #000;
			color: #FFFFFF;
		}
		
		.btn-hover{
			background-color: #000;
			color: #FFFFFF;
		}
		
		.btn:after {
			border: 0;
		}
		
		.bottom {
			text-align: center;
			color: rgba(97, 99, 113, 1);
			margin-top: 50rpx;
		}
	}
	
	
</style>
